package com.qeeyou.apps.android.craftsman.activity.login;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.common.Contants;
import com.qeeyou.apps.android.craftsman.event.LoginEvent;
import com.qeeyou.apps.android.craftsman.po.LoginInfo;
import com.qeeyou.apps.android.craftsman.service.SystemContext;
import com.qeeyou.apps.android.craftsman.service.UserService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends Activity {

    private LinearLayout llayoutLogin;
    private EditText editUsername;
    private EditText editPassword;

    private LinearLayout llayoutVerifyLogin;
    private EditText editMobile;
    private EditText editVerifyCode;

    private Button btnLogin;
    private Button btnForgetPassword;

    private Button btnSwitchLogin;
    private Button btnRegister;

    private CustomLoadingDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        setView();

        setEvent();
    }

    private void setView() {
        llayoutLogin = (LinearLayout) this.findViewById(R.id.llayoutLogin4Login);
        editUsername = (EditText) this.findViewById(R.id.editUserName4Login);
        editPassword = (EditText) this.findViewById(R.id.editPassword4Login);

        llayoutVerifyLogin = (LinearLayout) this.findViewById(R.id.llayoutVerifyLogin4Login);
        editMobile = (EditText) this.findViewById(R.id.editMobile4Login);
        editVerifyCode = (EditText) this.findViewById(R.id.editVerifyCode4Login);

        btnLogin = (Button) this.findViewById(R.id.btnLogin4Login);
        btnForgetPassword = (Button) this.findViewById(R.id.btnForgetPassword4Login);

        btnSwitchLogin = (Button) this.findViewById(R.id.btnSwitchLogin4Login);
        btnRegister = (Button) this.findViewById(R.id.btnRegister4Login);

        llayoutLogin.setVisibility(View.VISIBLE);
        llayoutVerifyLogin.setVisibility(View.INVISIBLE);
        btnSwitchLogin.setText(R.string.button_sms_login);

//        editUsername.setText("18621016491");
//        editUsername.setText("18521508353");
//        editPassword.setText("123456");

    }

    private void setEvent() {

        btnLogin.setOnClickListener(onLoginClickListener);

        btnForgetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
            }
        });

        btnSwitchLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (llayoutLogin.getVisibility() == View.VISIBLE) {
                    llayoutLogin.setVisibility(View.INVISIBLE);
                    llayoutVerifyLogin.setVisibility(View.VISIBLE);
                    btnSwitchLogin.setText(R.string.button_account_login);
                } else {
                    llayoutLogin.setVisibility(View.VISIBLE);
                    llayoutVerifyLogin.setVisibility(View.INVISIBLE);
                    btnSwitchLogin.setText(R.string.button_sms_login);
                }
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, Contants.RequestCode.REQUEST_REGISTER);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private View.OnClickListener onLoginClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username = editUsername.getText().toString();
            if (username == null || username.trim().equalsIgnoreCase("")) {
                Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            String password = editPassword.getText().toString();
            if (password == null || password.trim().equalsIgnoreCase("")) {
                Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            String[] params = {username, password};
            new UserLoginAction().execute(params);
        }
    };

    //用户登录
    public class UserLoginAction extends AsyncTask<String, Void, LoginInfo> {
        private String username;
        private String password;

        public UserLoginAction() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mProgressDialog == null) {
                mProgressDialog = new CustomLoadingDialog(LoginActivity.this);
            }
            mProgressDialog.setMessage("正在登录中...");
            mProgressDialog.show();
        }

        @Override
        protected void onPostExecute(LoginInfo loginInfo) {
            super.onPostExecute(loginInfo);
            if (mProgressDialog != null) {
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
            if (loginInfo != null) {
                SystemContext.getInstance().setUsername(username);
                SystemContext.getInstance().setPassword(password);
                SystemContext.getInstance().setLoginInfo(loginInfo);
                EventBus.getDefault().post(new LoginEvent());
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        LoginActivity.this.finish();
                    }
                }, 1000);
            } else {
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected LoginInfo doInBackground(String... params) {
            username = params[0];
            password = params[1];
            Map<String, String> parameter = new HashMap<>();
            parameter.put("username", username);
            parameter.put("password", password);
            LoginInfo info = UserService.getInstance().userLogin(parameter);
            return info;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Contants.RequestCode.REQUEST_REGISTER:
                if (resultCode == RESULT_OK) {
                    finish();
                }
                break;
        }
    }
}
